var $ = require('../../utils/Ajax.js');
Page({
  data: {
    // 顶部分类
    TopList: [],
    // 顶部分类下标
    ListIndex: 0,
    Experience: null,
    // 图片地址
    PictureUrl: getApp().PictureUrl,
    // 点击收藏更换图片
    Collection: "../../images/sc_nor.png",
    IsCollection: "../../images/sc_sel.png",
    PageI: 1,
    EndPage: null,
    Tid: 0,
    // 是否显示已经到底了
    InTheEnd: true
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: "体验列表"
    })
    var that = this;
    $.Ajax("api/x_list_exp", "GET", {}, function (res) {
      var TopList = res.d.list_tag.reverse();
      console.log(res.d.list_experience.data)
      that.setData({
        PageI: 1,
        TopList: TopList,
        Experience: res.d.list_experience.data,
        EndPage: res.d.list_experience.last_page
      })
    });
  },

  // 下拉到底部加载新数据
  onReachBottom: function () {
    var that = this;
    if (that.data.PageI == that.data.EndPage) {
      // wx.showToast({
      //   title: '已经到底了',
      //   icon: 'success',
      //   duration: 3000
      // });
      that.setData({
        InTheEnd: false
      })
    }else{
      var PageI = ++that.data.PageI;
      that.setData({
        PageI: PageI
      })
      $.Ajax("api/x_list_exp", "GET", {
        page: PageI,
        tid: that.data.Tid
      }, function (res){
        if (res.s) {
          that.setData({
            Experience: that.data.Experience.concat(res.d.list_experience.data)
          })
        }
      });
    }
  },

  // 监听用户下拉刷新
  onPullDownRefresh: function () {
    // 手机震动
    wx.vibrateShort();
    var that = this;
    $.Ajax("api/x_list_exp", "GET", {}, function (res) {
      that.setData({
        PageI: 1,
        ListIndex: 0,
        Experience: res.d.list_experience.data,
        EndPage: res.d.list_experience.last_page
      })
      wx.stopPullDownRefresh({
        success: function (res) {
          wx.showToast({
            title: '刷新成功',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
        }
      })
    });
  },

  // 点击顶部列表
  ClickList: function (e) {
    var that = this;
    var Index = e.currentTarget.dataset.listindex
    var id = e.currentTarget.dataset.id;
    this.setData({
      Tid: id,
      ListIndex: Index
    })
    $.Ajax("api/x_list_exp", "GET", {
      tid: id
    }, function (res) {
      console.log(res)
      that.setData({
        PageI: 1,
        Experience: res.d.list_experience.data,
        EndPage: res.d.list_experience.last_page
      })
    });
  },

  // 点击进入详情
  ExperDetails: function(e){
    var Id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../ExperDetails/ExperDetails?Id=' + Id,
    })
  },

  // 点击收藏
  Collection: function (e) {
    var that = this;
    var Token = wx.getStorageSync('Token');
    if (Token == '' || Token == null) {
      wx.switchTab({
        url: '../My/My',
      })
    } else {
      var Id = e.currentTarget.dataset.tid
      $.Ajax("api/x_add_coll", "POST", {
        id: Id,
        t: 1
      }, function (res) {
        if (res.s) {
          wx.showToast({
            title: res.m,
            icon: 'success',
            duration: 3000
          });
          var CollectionIndex = e.currentTarget.dataset.experienceindex;
          var is_collection = `Experience[${CollectionIndex}].is_collection`;
          if (that.data.Experience[CollectionIndex].is_collection == 1){
            that.setData({
              [is_collection]: 0
            })
          }else{
            that.setData({
              [is_collection]: 1
            })
          }
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'success',
            duration: 3000
          });
        }
      });
    }
  }
})